package com.zx.mes.disruptor.hyl.quickstart;

import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
import lombok.extern.java.Log;
import java.nio.ByteBuffer;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
 * disruptor instance
 *
 * @author huayunlong
 * @date 2018-9-5
 */
@Log
public class Main {
    public static void main(String[] args) {

        int ringBufferSize = 1024 * 1024;

        ThreadFactory threadFactory = Executors.defaultThreadFactory();

        /// disruptor instance and setting
        Disruptor<OrderEvent> disruptor = new Disruptor<>(new OrderEventFactory(),
                ringBufferSize,
                threadFactory,
                ProducerType.SINGLE,
                new BlockingWaitStrategy());

        /// bind consumer
        disruptor.handleEventsWith(new OrderEventHandler());
        /// get ringBuffer
        RingBuffer<OrderEvent> ringBuffer = disruptor.getRingBuffer();

        /// start
        disruptor.start();

        /// deliver data to ringBuffer
        OrderEventProducer producer = new OrderEventProducer(ringBuffer);

        ByteBuffer buffer = ByteBuffer.allocate(8);

        for ( long i= 0; i < 100; i++) {
            buffer.putLong(0, i);
            /// 下标移了3位
///            buffer.putInt(0, i);
            producer.send(buffer);
        }



///        log.info(String.valueOf(buffer.get(0)));

        disruptor.shutdown();

    }
}
